import { ServiceConfig, Guid } from '../src/common';
import { FBSDataContextBase, DataContextFactory } from '../src/domain/fbsDataContext';
import { OrderService } from '../src/service/orderService';
import { Order } from '../src/domain/model';
import { WorkshiftService } from '../src/service/workshiftService';

import * as assert from 'assert';


describe('workshiftService', function () {
    let ctx: FBSDataContextBase = DataContextFactory.GetDataContext();
    let orderSvr: OrderService = ServiceConfig.Current.GetService<OrderService>('orderService');;
    let workshiftSvr = ServiceConfig.Current.GetService<WorkshiftService>('workshiftService');
    let openTime = 1496652796867;
    let closeTime = 1496653271164;
    let user = {
        password: "11d5ca2dda045fb7455bd0f3e40f90e8",
        pic: "",
        registeredTime: 1462253790000,
        id: "7a5205f85f2d441092cea010cb3eb412",
        name: "乐妃",
        mobile: "18583920602"
    };
    let currentWorkshift = {
        clockin: {
            id: "7a5205f85f2d441092cea010cb3eb412",
            name: "乐妃"
        },
        clockout: null,
        openTime: 1496652796867,
        closeTime: null,
        terminal: {
            id: "5f39a22e83e4454eb8c93f827d8688ae",
            name: "P1"
        },
        totalSales: {
            amount: 0,
            quantity: 0
        },
        payments: [],
        orders: [],
        store: {
            id: "2a8mmqsc5kw0k4g00co4gko0c"
        },
        duration: "",
        workingFund: 0
    };

    async function clearTestData(openTime, closeTime) {
        // 清除该时间范围内的订单
        let orders = await orderSvr.GetWorkshiftOrders(openTime, closeTime);
        for (let order of orders) {
            await ctx.Delete(order);
        }
    }

    before(async function () {
        // 生成订单数据
        // 时间范围 1496652796867 1496653271164 8 分钟
        // 只添加必须属性
        // order: id checkout orderType amountDue status sourceid creator isCounter createTime
        // order.checkout: terminal.id payments
        // order.checkout.payment: channel amount createTime
        let terminalId = '5f39a22e83e4454eb8c93f827d8688ae';
        let creator = {
            id: '7a5205f85f2d441092cea010cb3eb412',
            name: '乐妃'
        }
        let items = [
            {
                id: Guid.GetGuid(),
                checkout: {
                    terminal: { id: terminalId },
                    payments: [
                        {
                            id: '15mt58pgql5nsjvyk0dbk2mc6x',
                            channel: 'cash',
                            amount: -1603.6,
                            createTime: 1496653040749
                        }
                    ]
                },
                orderType: 'refund',
                amountDue: -1603.6,
                status: 'closed',
                creator: creator,
                isCounter: false,
                createTime: 1496653036470
            },
            {
                id: Guid.GetGuid(),
                checkout: {
                    terminal: { id: terminalId },
                    payments: [
                        {
                            id: 'vk7p9gjq2eqylk7qrjiubscyf',
                            channel: 'cash',
                            amount: 1603.6,
                            createTime: 1496652939766
                        }
                    ]
                },
                orderType: 'sales',
                amountDue: 1603.6,
                status: 'refund',
                creator: creator,
                isCounter: false,
                createTime: 1496652937295
            },
            {
                id: Guid.GetGuid(),
                checkout: {
                    terminal: { id: terminalId },
                    payments: [
                        {
                            id: '1qmqrgjt880relhd7u2txs6g3d',
                            channel: 'cash',
                            amount: 200,
                            createTime: 1496652892090
                        }
                    ]
                },
                orderType: 'sales',
                amountDue: 200,
                status: 'macct-deposit',
                creator: creator,
                isCounter: false,
                createTime: 1496652892090
            },
            {
                id: Guid.GetGuid(),
                checkout: {
                    terminal: { id: terminalId },
                    payments: [
                        {
                            id: '162nh2ckpda9jvdeqyv74nkjuf',
                            channel: 'cash',
                            amount: 105.45,
                            createTime: 1496652865959
                        },
                        {
                            id: '19xw2748fby58xepdhk3u2dnb7',
                            channel: 'cash',
                            amount: -105.45,
                            createTime: 1496652907084
                        },
                        {
                            id: 'dieaj2nwdww53fi5fhw25pgyi',
                            channel: '银联',
                            amount: 50,
                            createTime: 1496652915136
                        },
                        {
                            id: 'ya2178hgnt9ihju6ms5y4wdhj',
                            channel: '银联',
                            amount: 55.45,
                            createTime: 1496652916150
                        }
                    ]
                },
                orderType: 'sales',
                amountDue: 105.45,
                status: 'closed',
                creator: creator,
                isCounter: true,
                createTime: 1496652853972
            },
            {
                id: Guid.GetGuid(),
                checkout: {
                    terminal: { id: terminalId },
                    payments: [
                        {
                            id: '1htcv6fsdnlq0y5sk1nfg1iyt3',
                            channel: 'cash',
                            amount: 41.8,
                            createTime: 1496652838775
                        }
                    ]
                },
                orderType: 'sales',
                amountDue: 41.8,
                status: 'closed',
                creator: creator,
                isCounter: false,
                createTime: 1496652834638
            }
        ]

        // 清除该时间范围内的订单
        await clearTestData(openTime, closeTime);
        // 写入订单数据
        for (let item of items) {
            let order = new Order;
            order = Object.assign(order, item);
            await ctx.Create(order);
        }

    });

    describe('测试交班信息生成方法', function () {
        it('workshiftSvr.generateWorkshiftInfo should be ok', async function () {
            let expectedData = {
                "clockin": {
                    "id": "7a5205f85f2d441092cea010cb3eb412",
                    "name": "乐妃"
                },
                "openTime": 1496652796867,
                "closeTime": 1496653271164,
                "duration": "8  分钟",
                "terminal": "5f39a22e83e4454eb8c93f827d8688ae",
                "totalSales": {
                    "quantity": 5,
                    "amount": 347.25
                },
                "payments": [
                    {
                        "channel": "cash",
                        "amount": 241.8,
                        "quantity": 6
                    },
                    {
                        "channel": "银联",
                        "amount": 105.45,
                        "quantity": 2
                    }
                ],
                "paymentsDetail": [
                    {
                        "channel": "cash",
                        "amount": 1845.4,
                        "quantity": 5,
                        "txType": "pay"
                    },
                    {
                        "channel": "银联",
                        "amount": 105.45,
                        "quantity": 2,
                        "txType": "pay"
                    },
                    {
                        "channel": "cash",
                        "amount": -1603.6,
                        "quantity": 1,
                        "txType": "refund"
                    }
                ],
                "orders": [
                    {
                        "orderType": "refund",
                        "quantity": 1,
                        "amount": -1603.6
                    },
                    {
                        "orderType": "sales",
                        "quantity": 4,
                        "amount": 1950.85
                    }
                ],
                "workingFund": 0
            };
            let result = await workshiftSvr.generateWorkshiftInfo(closeTime, user, currentWorkshift);
            delete result.id;
            delete result.store;
            assert.deepEqual(result, expectedData, '生成交班信息失败');

            after(async function () {
                await clearTestData(openTime, closeTime);
            })
        });
    });
});